﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebSocketSharp.Server;
using System.Net;
using PServiceBus.Core.Runtime;

namespace PServiceBus.SharpWebSocket {
    public class SharpWebSocketServer : PServiceBus.Core.Interface.IWebSocketServer {

        internal static PServiceBus.Core.Interface.IWebSocketServer Current;

        #region IWebSocketServer Members

        private WebSocketServer server;
        public void Start(string host, int port, bool secured) {
            server = new WebSocketServer(IPAddress.Any, port, secured);
            server.AddService<SharpWebSocketService>("/");
            Current = this;
            server.Start();
        }

        public Action<Core.Interface.IWebSocketConnection, string> OnMessage { get; set; }

        public Action<Core.Interface.IWebSocketConnection, Exception> OnError { get; set; }

        public Action<Core.Interface.IWebSocketConnection> OnOpen { get; set; }

        public Action<Core.Interface.IWebSocketConnection> OnClose { get; set; }

        public string Cerfx { get; set; }

        public void Close() {
            if (server == null) return;
            ShellHelper.NetShRemoveUrlAcl(server.Port);
            server.Stop();
        }

        #endregion
    }
}
